package com.dao.impl;

import com.dao.OrderinfoDao;
import com.entity.Orderinfo;
import com.entity.OrderinfoExample;
import com.mapper.OrderinfoMapper;
import com.page.PageInfo;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class OrderinfoDaoImpl implements OrderinfoDao {
    @Resource
    OrderinfoMapper orderinfoMapper;

    /**
     * 根据订单编号查询一个订单
     * @param orid  订单编号
     * @return 订单对象
     */
    public Orderinfo getByOrid(String orid){
        return orderinfoMapper.selectByPrimaryKey(orid);
    }
    public List<Orderinfo> getAll(){
        return orderinfoMapper.getAll();
    }
    public List<Orderinfo> getAllByPage(PageInfo pageInfo){
        int currPage = pageInfo.getCurrPage();
        int pageSize = pageInfo.getPageSize();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", currPage);
        map.put("pageSize", pageSize);
        System.out.println(pageInfo.getCurrPage() + " "+ pageInfo.getPageSize());
        //调用示例查询得到当前的总记录数
        pageInfo.setItemCount((int) orderinfoMapper.countByExample(new OrderinfoExample()));
        return orderinfoMapper.getAllByPage(map);
    }

    @Override
    public List<Orderinfo> searchNameByPage(PageInfo pageInfo,String adname) {

        OrderinfoExample orderinfoExample = new OrderinfoExample();
        int limit = pageInfo.getPageSize();
        int offset = (pageInfo.getCurrPage() - 1) * limit;
        orderinfoExample.setOffset(offset);//就是原来的firstResult 就是通过（currPage - 1） * pageSize
        orderinfoExample.setLimit(limit);
        //createCriteria 创建一个条件查询
        //orderinfoExample.createCriteria().andAdnameLike(adname);
        return orderinfoMapper.selectByExample(orderinfoExample);
    }

    /**
     * 根据主键查询
     * @param orid
     * @return
     */
    public Orderinfo getOrderinfoByOrid(String orid){
        return orderinfoMapper.selectByPrimaryKey(orid);
    }

    /**
     * 添加一个实体
     * @param orderinfo
     * @return
     */
    public int addOrderinfo(Orderinfo orderinfo){
        return orderinfoMapper.insertSelective(orderinfo);
    }
    /**
     * 修改实体,如果有条件 需要设置example
     */
    public int updateOrderinfo(Orderinfo orderinfo){
        return orderinfoMapper.updateByPrimaryKeySelective(orderinfo);
    }
    public int updateOrderinfoByExample(Orderinfo orderinfo){
        return orderinfoMapper.updateByExample(orderinfo,null);
    }

    /**
     * 删除一个实体
     * @param adid
     * @return
     */
    public int deleteOrderinfoByPrimayKey(String adid){
        return orderinfoMapper.deleteByPrimaryKey(adid);
    }

    /**
     * 删除实体根据示例
     * @return
     */
    public int deleteOrderinfoByExample(){
        return orderinfoMapper.deleteByExample(null);
    }

   public List<Orderinfo> getByBuid(String buid){
       return orderinfoMapper.getByBuid(buid);
    }
}
